package com.yvan;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by YuanHua on 2017/5/10.
 */
public class DataUtils {

    public static String convBigDecimalToString(BigDecimal number, int decimal) {
        if (number == null) return "";
        StringBuilder pattern = new StringBuilder("#");
        if (decimal > 0) {
            pattern.append(".");
            for (int i = 0; i < decimal; i++) {
                pattern.append("0");
            }
        }
        DecimalFormat format = new DecimalFormat(pattern.toString());
        return format.format(number);
    }

    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    public static void main(String[] a) {
        System.out.println(convBigDecimalToString(new BigDecimal(5656.00000), 3));
        System.out.println(subZeroAndDot(convBigDecimalToString(new BigDecimal(5656.00000), 3)));
    }
}
